package com.rule.easyrule.config;

import com.rule.easyrule.entity.BizOrder;
import lombok.SneakyThrows;
import org.jeasy.rules.api.Facts;
import org.jeasy.rules.api.Rules;
import org.jeasy.rules.api.RulesEngine;
import org.jeasy.rules.core.DefaultRulesEngine;
import org.jeasy.rules.support.composite.ActivationRuleGroup;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@RestController
@RequestMapping("/order")
public class OrderTest {

    @PostMapping("/annotation")
    @SneakyThrows
    public void annotationTest(@RequestBody BizOrder order) {
        //定义数据
        Facts facts = new Facts();
        facts.put("order", order);
        ActivationRuleGroup activationRuleGroup = new ActivationRuleGroup("order_type_rule", "订单类型规则");
        activationRuleGroup.addRule(new OrdinaryOrderRule());
        activationRuleGroup.addRule(new DiscountOrderRule());
        //注册
        Rules rules = new Rules();
        rules.register(activationRuleGroup);
        //启动点火
        RulesEngine rulesEngine = new DefaultRulesEngine();
        rulesEngine.fire(rules, facts);
    }
}